package top.itaso.app.ui.storage

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import top.itaso.lib.widget.vo.Node
import top.itaso.lib.common.FileUtils
import top.itaso.lib.common.StorageVolumeUtils
import top.itaso.lib.common.StorageVolumeUtils.UStorageVolume
import java.util.*

/**
 * @Description:
 * @Author dengfu.su
 * @Date 2023/3/6
 */
class FolderTreeViewModel(application: Application) : AndroidViewModel(application) {
    private val TAG = "FolderTreeViewModel"
    val infoDetail: LiveData<String>
        get() = _infoDetail

    val folders: LiveData<ArrayList<Node<UStorageVolume>>>
        get() = _folders

    private val _infoDetail = MutableLiveData<String>()
    private val _folders = MutableLiveData<ArrayList<Node<UStorageVolume>>>()

    init {
//        _infoDetail.value = StorageVolumeUtils.getAppPrivateDir(getApplication())
        val folders = ArrayList<Node<UStorageVolume>>()
        val uStorageVolume = StorageVolumeUtils.getPrivateStorageVolume(getApplication())
        folders.add(
            Node<UStorageVolume>(
                uStorageVolume.fsUuid,
                "-1",
                if (uStorageVolume.label.isNullOrEmpty()) {
                    uStorageVolume.fsLabel
                } else {
                    uStorageVolume.label
                },
                uStorageVolume
            )
        )
        _folders.value = folders
    }

    fun refreshChildFolder(folderNodeTreeBean: Node<Any>) {
        val targetFilePath = (folderNodeTreeBean.data as UStorageVolume).path
        val targetFileIsFolder = FileUtils.isFolder(targetFilePath)
        Log.d(
            TAG,
            "refreshChildFolder: >>>>>>targetFileIsFolder=$targetFileIsFolder,targetFilePath=$targetFilePath"
        )
        if (!targetFileIsFolder) {
            return
        }
        val childfiles = FileUtils.getFileList(targetFilePath)
        val dataNodes = _folders.value!!
        childfiles?.forEach { childFile ->
            val uStorageVolumeChild = UStorageVolume()
            uStorageVolumeChild.path = childFile.path
            uStorageVolumeChild.internalPath = uStorageVolumeChild.path
            uStorageVolumeChild.fsUuid = uStorageVolumeChild.path
            uStorageVolumeChild.label = FileUtils.getFileByDir(uStorageVolumeChild.path).name
            uStorageVolumeChild.fsLabel = uStorageVolumeChild.label
            var hasExit = false // childFile 是否在 dataNote 数据中，是，则更新 UStorageVolume 内容，否，则新增进去
            dataNodes?.forEach { dataNote ->
                if (childFile.path.equals((dataNote.data as UStorageVolume).path)) {
                    dataNote.data = uStorageVolumeChild
                    hasExit = true
                    return@forEach
                }
            }
            if (!hasExit) {
                val childNode = Node(
                    uStorageVolumeChild.path,
                    folderNodeTreeBean.id,
                    if (uStorageVolumeChild.label.isNullOrEmpty()) {
                        uStorageVolumeChild.fsLabel
                    } else {
                        uStorageVolumeChild.label
                    },
                    uStorageVolumeChild
                )
                childNode.isExpand = true
                childNode.isChecked = folderNodeTreeBean.isChecked
                dataNodes.add(childNode)
            }

        }
        _folders.value = dataNodes
    }
}